-- Options object which several method can be called globally
DKPMSopt = {}
DKPMSopt.chosenPlayerName = "Initialization ERROR"
DKPMSopt.chosenPlayerDKP = 0
DKPMSopt.raid_length = 0
DKPMSopt.raidAdjustment = 0



-- used for a select box named "playernames" which contains a grafical view of the players registrered by the addon.
local playerbase = {["Empty"] = "Error"}
local optionsframe = nil


local adjustRaidDKP = {
		type = "group",
		name = "Raid",
		desc = "Adjust DKP for raids",
		inline = false,
		args = {
			canceledRaidGroup = {
				name = "Cancelled raids",
				type = "group",
				inline = true,
				args = {
					canceledRAID = {
						name = "Cancelled raid",
						desc = "Creates a new raid and overwrite any raid already created for today. DKP awarded for prior raids today will be rolled back. This option will calculate Attendance and DKP for the players currently in the raid and Waiting list at the time of execution.",
						type = 'execute',
						confirm = function () return "Any raid created today will be overridden and awarded DKP reverted." end,													
						func = function (info, value) 
									if (DKPMSopt:GetRaidLength() > 0 ) then
										DKPMS:CreateCancelledRaid(DKPMSopt:GetRaidLength())
									else
										DKPMS:Warning("Enter any amount above 0 and only numbers")
									end   
							   end												
								}, --canceledRAID end
					attendanceMinutes = {
						name = "Raid length",
						desc = "Amount of minutes the raid should calculate DKP and attendance.",
						type = 'input',
						get = function(info) return tostring(DKPMSopt:GetRaidLength()) end,
						set = function(info, value) DKPMSopt:SetRaidLength(value) end
									  }, -- attendanceMinutes end
					
					},--canceledRaidGroup args end
			 },
			 currentRaidAdjustment = {
				name = "Adjust raid DKP",
				type = "group",
				inline = true,
				args = {
					input = {
							name = "DKP",
							desc = "Amount of DKP to add/substract from the current raid IMPORTANT: If the negative number is greater then the awarded dkp, the raid dkp and attendance will go below 0. Use this function carefully",
							type = "input",
							get = function (info) return tostring(DKPMSopt:GetRaidAdjustment()) end,
							set = function (info, value) DKPMSopt:SetRaidAdjustment(value) end
							},
					butten = {
							name = "Execute adjustment",
							desc = "Executes the adjustment to the current raid",
							type = "execute",
							func = function (info, value) 
										DKPMS:AdjustRaidDkp(DKPMSopt:GetRaidAdjustment()) 
										DKPMSopt:SetRaidAdjustment()
								   end
							 },
						},-- 
				},	
		},
	}

local adjustPlayerDKP = {
			name = "Player",
			desc = "Adjust a players DKP",
			type = "group",
			args = {
					 awardPlayerDKP = {
						name = "Manipulate Player DKP",
						type = "group",
						inline = true,
						args = {
								playernames = {
									name = "Select a player",
									type = "select",
									desc = "A list of players main charactors",											
									order = 1,
									values = playerbase, 
									get = function (info) return DKPMSopt:GetChosenPlayer()  end,
									set = function (info, value)  
											local tempData = AllRealmsDKPData["Arathor"]
											tempData = tempData["Timbo"]
											DKPMSopt:SetChosenPlayerDKP(tempData[value].DKP)
											DKPMSopt:SetChosenPlayer(value)
											
										  end,
								}, --playernames end
								playerDKP = {
									name = "DKP",
									type = "input",
									desc = "Selected players current DKP",											
									order = 2,
									get = function (info) return tostring(DKPMSopt:GetChosenPlayerDKP())  end,
									set = function (info, value) DKPMSopt:SetChosenPlayerDKP(value) end,
								}, --playerDKP end								
								changePlayerDKP = {
									name = "Save new DKP pool",
									type = "execute",
									desc = "Saves the new DKP value for selected player",											
									order = 3,
									confirm = function () 
													local player = DKPMSopt:GetChosenPlayer()
													if (AllRealmsDKPData["Arathor"]["Timbo"][player] ~= nil and DKPMSopt:GetChosenPlayerDKP() ~= nil) then																	
														local differance = DKPMSopt:GetChosenPlayerDKP() - AllRealmsDKPData["Arathor"]["Timbo"][player].DKP 
														if (differance >= 0) then	
															return "Increase "..DKPMSopt:GetChosenPlayer().."'s DKP pool with "..differance
														else
															return "Decrease "..DKPMSopt:GetChosenPlayer().."'s DKP pool with "..differance
														end
													else
														return false
													end														
												  end,
									func = function() DKPMSopt:ManipulateDKPpool(DKPMSopt:GetChosenPlayer(), DKPMSopt:GetChosenPlayerDKP()) end,
								}, --changePlayerDKP end				
						}, -- awardPlayerDKP args end
					 }, -- awardPlayerDKP end				
			},
		}
	--program the options menu to replace the current options menu, remember to remove the old one
	local options = {
				type = 'group', 
				args = {
					manipulateRaid = {
						name = "DKP and Attendance",
						desc = "Handle DKP and Attendance, This group enables you to add/substract DKP from a single player or a whole raid (More will be added in later builds)",
						type = 'group', 
						inline = false,
						args = {	
								raidStartBonus = {
									name = "Raid Start DKP bonus",
									desc = "Amount of DKP awarded for players in raid when raid starts",
									type= "input",
									order = 1,
									get = function (info) return tostring(zKP_options.StartBonus) end,
									set = function(info, value) if (value ~= nil and value ~= "") then zKP_options.StartBonus = tonumber(value) end end,
												 },
								raidEndBonus = {
									name = "Raid End DKP bonus",
									desc = "Amount of DKP awarded for players in raid when raid ends",
									type= "input",
									order = 2,
									get = function (info) return tostring(zKP_options.EndBonus) end,
									set = function(info, value) if (value ~= nil and value ~= "") then zKP_options.EndBonus = tonumber(value) end end,
												 },
								minimumAttendancePercentage = {
									name = "Minimum Attendance",
									desc = "Minimuim Attendance players need to bid on items as members",
									type= "input",
									order = 3,
									get = function (info) return tostring(zKP_options.bid_attendance_percent) end,
									set = function(info, value) if (value ~= nil and value ~= "") then zKP_options.bid_attendance_percent = tonumber(value) end end,
															  },
								raidUpdateInterval = {
									name = "DKP Frequence",
									desc = "This options defined the frequency of when players are awarded dkp measured in minutes.",
									type= "input",
									order = 4,
									get = function (info) return tostring(zKP_options.UpdateInterval) end,
									set = function(info, value) if (value ~= nil and value ~= "") then zKP_options.UpdateInterval = tonumber(value) end end,
												 },
								raidFrequencyDKP = {
									name = "DKP per Frequence",
									desc = "Amount of DKP awarded to each player in raid per DKP Frequence",
									type= "input",
									order = 5,
									get = function (info) return tostring(zKP_options.frequentDKP) end,
									set = function(info, value) if (value ~= nil and value ~= "") then zKP_options.frequentDKP = tonumber(value)  end end,
												 },								
							group1 = adjustRaidDKP,
							group2 = adjustPlayerDKP,


							   }, -- args end
							
					},--manipulateRaid end

				},--args end
		}--options end


function DKPMSopt:LoadOptions()
	

	if (zKP_options == nil) then	
		zKP_options = {}
	end

	if (zKP_options.bid_attendance_percent == nil) then	
		zKP_options.bid_attendance_percent = 0 -- percent attendance a player must have to be able to bid as a full member of the guild.
	end
	
	if (zKP_options.UpdateInterval == nil) then	
		zKP_options.UpdateInterval = 1;
	end

	if (zKP_options.frequentDKP == nil) then	
		zKP_options.frequentDKP    = 1;
	end
	
	if (zKP_options.StartBonus == nil) then	
		zKP_options.StartBonus     = zKP_StartBonus; 
	end

	if (zKP_options.EndBonus == nil) then	
		zKP_options.EndBonus       = zKP_EndBonus; 
	end

	DKPMSopt:LoadPlayers()

	LibStub("AceConfig-3.0"):RegisterOptionsTable("DKPManager Server", options, nil)
	optionsframe = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("DKPManager Server", "DKPManager Server")

end

function DKPMSopt:OptionsShow()

	if (optionsframe ~= nil) then	
		InterfaceOptionsFrame_OpenToCategory(optionsframe)
	end

end


function DKPMSopt:LoadPlayers()
	local tempPlayerBase = AllRealmsDKPData["Arathor"]

	for key, value in pairs(tempPlayerBase["Timbo"]) do
	    playerbase[key] = tostring(key)	
	end	

end

function DKPMSopt:ManipulateDKPpool(player, amount)
	if (player ~= nil and amount ~= nil and player ~= "Initialization ERROR") then	
		AllRealmsDKPData["Arathor"]["Timbo"][tostring(player)].DKP = tonumber(amount)

		local temp = AllRealmsDKPData["Arathor"]["Timbo"][tostring(player)].update_Value
			temp = temp +1
			AllRealmsDKPData["Arathor"]["Timbo"][tostring(player)].update_Value = temp
			DKPMSopt:SetChosenPlayerDKP( AllRealmsDKPData["Arathor"]["Timbo"][tostring(player)].DKP )

	end
end




---------------------------------------------
------------ GET/SET FUNCTIONS---------------
---------------------------------------------

function DKPMSopt:SetChosenPlayer(value)
	if (value == nil) then	
		value = "ERROR"
	end
	DKPMSopt.chosenPlayerName = value
end

function DKPMSopt:GetChosenPlayer()
	return DKPMSopt.chosenPlayerName
end

function DKPMSopt:SetChosenPlayerDKP(value)
	if (value == nil) then	
		value = 0
	end
	DKPMSopt.chosenPlayerDKP = tonumber(value)
end

function DKPMSopt:GetChosenPlayerDKP()
	return DKPMSopt.chosenPlayerDKP
end

function DKPMSopt:SetRaidLength(value)
	if (value == nil) then	
		value = 0
	end
	DKPMSopt.raid_length = tonumber(value)
end

function DKPMSopt:GetRaidLength()
	return DKPMSopt.raid_length
end

function DKPMSopt:GetRaidAdjustment()
	return DKPMSopt.raidAdjustment
end

function DKPMSopt:SetRaidAdjustment(value)
	if (value == nil) then	
		value = 0
	end

	DKPMSopt.raidAdjustment = value
end

